<?php

Yii::import('lib.ext.pay.alipay.AlipayService');
Yii::import('lib.ext.pay.alipay.AlipayNotify');
Yii::import('lib.ext.pay.alipay.AlipaySubmit');
require_once Yii::getPathOfAlias('lib.ext.pay.alipay.AlipayCore').'.php';

class Alipay extends CComponent{
	
	public $aliapy_config;
	
	public $paymentId;
	
	public $paymentKey;
	
	public $paymentAccount;
	
	public $returnUrl;
	
	public $sign_type = 'MD5';
	
	public $input_charset = 'utf-8';
	
	public $transport = 'http';
	
	protected $_tid;
	
	protected $_price;
	
	protected $_notify = array();
	
	public function getConfig(){
		$aliapy_config['partner']      = $this->paymentId;
		//安全检验码，以数字和字母组成的32位字符
		$aliapy_config['key']          = $this->paymentKey;
		//签约支付宝账号或卖家支付宝帐户
		$aliapy_config['seller_email'] = $this->paymentAccount;
		//页面跳转同步通知页面路径，要用 http://格式的完整路径，不允许加?id=123这类自定义参数
		$aliapy_config['return_url']   = $this->returnUrl;
		//服务器异步通知页面路径，要用 http://格式的完整路径，不允许加?id=123这类自定义参数
		$aliapy_config['notify_url']   = $this->returnUrl;
		//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
		//签名方式 不需修改
		$aliapy_config['sign_type']    = $this->sign_type;
		//字符编码格式 目前支持 gbk 或 utf-8
		$aliapy_config['input_charset']= $this->input_charset;
		//访问模式,根据自己的服务器是否支持ssl访问，若支持请选择https；若不支持请选择http
		$aliapy_config['transport']    = $this->transport;	
		return $aliapy_config;
	}
	
	public function getTid(){
		return empty($this->_tid) ? $this->_tid = date('Ymdhis') : $this->_tid;
	}
	
	public function pay($transaction_id,$product_name,$order_price){
		$out_trade_no = $transaction_id;
		$subject = $product_name;
		$total_fee = $this->_price = $order_price;
		$aliapy_config = $this->getConfig();
		$parameter = array(
				"service"			=> "create_direct_pay_by_user",
				"payment_type"		=> "1",
				
				"partner"			=> trim($aliapy_config['partner']),
				"_input_charset"	=> trim(strtolower($aliapy_config['input_charset'])),
		        "seller_email"		=> trim($aliapy_config['seller_email']),
		        "return_url"		=> trim($aliapy_config['return_url']),
		        "notify_url"		=> trim($aliapy_config['notify_url']),
				
				"out_trade_no"		=> $out_trade_no,
				"subject"			=> $subject,
				"total_fee"			=> $total_fee,
		);
		//构造即时到帐接口
		$alipayService = new AlipayService($aliapy_config);
		$html_text = $alipayService->create_direct_pay_by_user($parameter);
		echo $html_text;
	}
	
	public function getParams(){
		return Yii::app()->request->isPostRequest ? $_POST : $_GET;
	}
	
	public function getPrice(){
		return $this->_price;
	}
	
	public function verify($alipayNotify){
		return Yii::app()->request->isPostRequest ? $alipayNotify->verifyNotify() : $alipayNotify->verifyReturn();
	}
	
	public function getNotify(){
		$aliapy_config = $this->getConfig();
		$params = $this->getParams();
		$params['notify_id'] = urlencode($params['notify_id']);
		$alipayNotify = new AlipayNotify($aliapy_config);
		$result = array();
		$result['code'] = false;
		if(empty($this->_notify) && $this->verify($alipayNotify)) {		//验证成功
		    $out_trade_no	= $this->_tid = $params['out_trade_no'];	    //获取订单号
		    $trade_no		= $params['trade_no'];	    					//获取支付宝交易号
		    $total_fee		= $this->_price = $params['total_fee'];			//获取总价格
		    																//交易成功结束
		    if($params['trade_status'] == 'TRADE_FINISHED' || $params['trade_status'] == 'TRADE_SUCCESS') {
		    	$result['tid'] = $out_trade_no;
		    	$result['price'] = $total_fee;
		    	$result['code'] = true;
		    }
		    $this->_notify = $result;
		}
		return $result['code'];
	}
	
}

?>